﻿//Globle var
var isBook, isFinalize, isMailC, isMailD, isSMSC, isSMSD, actionType;
var _guideXml = "";
var _domainXml = "";
//功能：页面加载
function win_onload() {
    var arg = window.dialogArguments;
    var domainXmlDoc = arg[0] == undefined ? "" : arg[0];
    var strHandleText = arg[1] == undefined ? "" : arg[1];

    var action = "GetStepPassGuideXML";

    var sParams = new StringBuilder();
    sParams.append("/MyWorkflow/WF_Engine_XmlHttp.aspx?stepGUID=" + $id("__stepGUID").value);
    if ($id("__lastStationGUID")) sParams.append("&lastStationGUID=" + $id("__lastStationGUID").value);


    switch ($id("__type").value) {
        case "[会签-提交]":
            actionType = "CounterSignPass";
            break;
        case "[审批-同意]":
            actionType = "ApprovePass";
            break;
        case "[发起人指派责任人]":
            actionType = "InitiatorAssign";
            action = "GetOwnerAssignGuideXML";
            $id("txtHandleText").value = strHandleText;
            break;
        case "[发起流程]":
            actionType = "ApprovePass";
            //发起不可用
            $id("imgCCSelect").disabled = true;
            $id("imgCCSelect").style.cursor = "default";
            $id("imgCCSelect").src = "/_imgs/selectOn.gif";
            $id("imgCCSelect").style.display = "none";
            $id("tbCC").style.tableLayout = "auto";
            $id("tdCCUser2").style.width = "20"; $id("txtHandleText").value = strHandleText;
            break;
        case "[重新发起]":
            actionType = "ReInitiate";
            break;
        default:
            $id("divHead").innerHTML = "";
            $id("divTitle").innerHTML = "";
            break;
    }


    _guideXml = GetDataFromXMLHTTP(sParams.toString(), action, "", "", domainXmlDoc == null ? "" : domainXmlDoc.xml);
    _domainXml = domainXmlDoc.xml;

    var Dom = XMLDOM();
    Dom.loadXML(_guideXml);
    var node = null;

    if (Dom.parseError.errorCode == 0) {
        node = Dom.documentElement.selectSingleNode("MsgMask");
        if (node != null) {
            isMailC = node.getAttribute("MailC");
            isMailD = node.getAttribute("MailD");
            isSMSC = node.getAttribute("SMSC");
            isSMSD = node.getAttribute("SMSD");
            $id("sendMail").checked = isMailC == "1" ? true : false;
            $id("sendMail").disabled = isMailD == "1" ? true : false;
            $id("sendSMS").checked = isSMSC == "1" ? true : false;
            $id("sendSMS").disabled = isSMSD == "1" ? true : false;
        }
        node = Dom.documentElement.selectSingleNode("AutoBook");
        if (node != null) isBook = node.text;

        node = Dom.documentElement.selectSingleNode("Finalize");
        if (node != null) isFinalize = node.text;
        //取定义的抄送人员
        node = Dom.documentElement.selectSingleNode("CC");

        if (node != null) {
            $id("txtCCUserGUID").value = node.getAttribute("CCUserGUID");
            $id("txtCCUserName").value = node.getAttribute("CCUserName");
        }

        //只有分支步骤才参与计算流转条件，其余如“结束步骤”、“修改后续步骤”、“由[发起人]指定下一步骤”）不参与计算
        var items = Dom.selectNodes("//Relation[@otype>-1]");
        var j = items.length;
        if (j > 0) {
            //循环每一分支，计算流转条件
            for (var i = 0; i < j; i++) {
                var sExp = "";
                if (items[i].attributes.getNamedItem("ExpressionStru"))
                    sExp = items[i].attributes.getNamedItem("ExpressionStru").text; 	//流转条件结构

                items[i].setAttribute("result", Calculate(sExp, domainXmlDoc));
            }


            //仅移除不满足条件的分支（步骤属性定义优先于条件 2009-04-15）
            for (var i = 0; i < j; i++) {
                if (items[i].attributes.getNamedItem("result").text == "false") {
                    items[i].parentNode.removeChild(items[i]);
                }
            }
            /*
            //判断
            if(Dom.selectNodes("//Relation[@result='true']").length>0)
            {
            //有满足条件的分支
            //仅显示满足条件的分支，需移除其余分支
            for(var i=0;i<j;i++)
            { 
            if(items[i].attributes.getNamedItem("result").text!="true")
            {
            items[i].parentNode.removeChild(items[i]);
            }
            }
            }
            else
            {
            //没有满足条件的分支或仅有条件为空的分支
            //移除不满足条件的分支
            for(var i=0;i<j;i++)
            { 
            if(items[i].attributes.getNamedItem("result").text=="false")
            {
            items[i].parentNode.removeChild(items[i]);
            }
            }
            }
            */
        }

        //根据处理的结果，生成步骤下拉选项
        var relation = Dom.selectNodes("//Relation");
        var k = relation.length
        if (k > 0) {
            var obj = document.getElementById("ddlNextStep");

            if (k > 1) {
                oOption = document.createElement("OPTION");
                oOption.text = "----请选择----";
                oOption.value = "";
                obj.options.add(oOption, obj.options.length);
            }
            for (var i = 0; i < k; i++) {
                var sStepName = new StringBuilder();

                if (relation[i].attributes.getNamedItem("StepID") != null && relation[i].attributes.getNamedItem("StepID").text.length > 0) {
                    sStepName.append("第");
                    sStepName.append(relation[i].attributes.getNamedItem("StepID").text);
                    sStepName.append("步：");
                }
                sStepName.append(relation[i].attributes.getNamedItem("NextStepName").text);
                if (relation[i].attributes.getNamedItem("StepType").text == "3" && isBook == "1") {
                    sStepName.append("(自动归档)");
                }

                oOption = document.createElement("OPTION");
                oOption.text = sStepName.toString();
                oOption.value = relation[i].attributes.getNamedItem("NextStepGUID").text;
                obj.options.add(oOption, obj.options.length);
            }
            obj.selectedIndex = 0;

        }
        else {
            //需要删除带出的抄送人
            $id("txtCCUserGUID").value = "";
            $id("txtCCUserName").value = "";
        }

    }

    delete (Dom);

    //选择下一步骤的相关处理
    chgStep();
}

//功能：改变下一步骤
function chgStep() {
    if (trStep.style.display == "none") return;

    var objStep = $id("ddlNextStep");

    if (objStep.options.length > 0) {
        tdAlert.innerText = "";
    }
    else {
        alert("当前流程步骤无满足条件的分支步骤，请联系流程创建人！");
        parent.window.close();
        return;
    }

    var Dom = XMLDOM(); //建立XMLDOM对象
    Dom.loadXML(_guideXml);
    if (Dom.parseError.errorCode != 0) return;  //检查是否发生获取数据时错误

    //恢复默认设置
    $id("tdAuditor").className = "req";
    $id("txtNextAuditorNames").style.backgroundColor = "";
    $id("txtNextAuditorNames").style.borderColor = "";
    $id("selectuser").style.display = "none";
    $id("selectauditor").style.display = "";
    $id("notselect").style.display = "none";
    $id("selectuser").ismultiselect = "false";
    $id("selectuser").isallowsort = "false";
    //	            $id("txtCCUserGUID").value = "";
    //	            $id("txtCCUserName").value = "";
    //取定义的抄送人员
    node = Dom.documentElement.selectSingleNode("CC");
    if (node != null && node.getAttribute("CCUserGUID") != "") {
        $id("txtCCUserGUID").value = node.getAttribute("CCUserGUID");
        $id("txtCCUserName").value = node.getAttribute("CCUserName");
    }

    //判断下一步的方式
    var sNextStepGUID = "";
    if (objStep.selectedIndex >= 0) sNextStepGUID = objStep.options[objStep.selectedIndex].value;

    switch (sNextStepGUID) {
        case "": 				//请选择
            sMode = "";
            $id("txtNextAuditors").value = "";
            $id("txtNextAuditorNames").value = "";
            $id("selectuser").style.display = "none";
            $id("selectauditor").style.display = "none";
            $id("notselect").style.display = "";
            break;

        case "NewStep": 			//另行指定责任人
            sMode = "NewStep";
            $id("txtNextAuditors").value = "";
            $id("txtNextAuditorNames").value = "";
            $id("selectuser").style.display = "";
            $id("selectauditor").style.display = "none";
            $id("notselect").style.display = "none";
            break;

        case "InitMemberAssign": //发起人指定下一步骤
            sMode = "InitMemberAssign";
            $id("txtNextAuditors").value = "";
            $id("txtNextAuditorNames").value = "[发起人]";
            $id("selectuser").style.display = "none";
            $id("selectauditor").style.display = "none";
            $id("notselect").style.display = "";
            break;

        default: 				//选择下一步骤					    
            $id("txtNextAuditors").value = "";
            $id("txtNextAuditorNames").value = "";
            $id("selectuser").style.display = "none";
            $id("selectauditor").style.display = "";
            $id("notselect").style.display = "none";


            var item = Dom.selectSingleNode("//Relation[@NextStepGUID='" + sNextStepGUID + "']");

            if (item != null) {
                sMode = item.attributes.getNamedItem("Mode").text;

                var auditors = item.attributes.getNamedItem("Auditor").text;

                $id("txtNextAuditors").value = auditors;
                $id("txtNextAuditorNames").value = item.attributes.getNamedItem("AuditorName").text;


                if (item.attributes.getNamedItem("AuditorCount")) {
                    //如果满足范围的责任人只有一个，就不用选择了
                    if (item.attributes.getNamedItem("AuditorCount").text == "1") {
                        $id("notselect").style.display = "";
                        $id("selectauditor").style.display = "none";
                    }

                    //无满足范围的责任人，就任意指定一个
                    if (item.attributes.getNamedItem("AuditorCount").text == "0" && objStep.options[objStep.selectedIndex].text != "指回当前步骤") {
                        tdAlert.innerText = $id("tdAuditor").className == "req" ? "提示：该步骤无可选责任人，请任意指定一个。" : "";
                        $id("selectuser").style.display = "";
                        $id("selectuser").ismultiselect = (item.attributes.getNamedItem("IsMulti") && item.attributes.getNamedItem("IsMulti").text == "1" ? "true" : "false");
                        $id("selectuser").isallowsort = (item.attributes.getNamedItem("IsMulti") && item.attributes.getNamedItem("IsMulti").text == "1" ? (item.attributes.getNamedItem("StepType").text == "1" ? "true" : "false") : "false");
                        $id("selectauditor").style.display = "none";
                        $id("notselect").style.display = "none";
                    }
                }

                //自动归档附加处理
                if (isBook == "1" && sMode == "NextToBook") {
                    //判断是否定稿
                    if (isFinalize != "1" && ($id("__DocType").value == "1" || $id("__DocType").value == "3")) {
                        if ($id("__type").value == "[发起人指派责任人]") {
                            alert("归档步骤将自动完成，请指回当前步骤对文档进行定稿！");
                            objStep.selectedIndex = 0;
                            chgStep();
                            return false;
                        }
                        else {
                            alert("归档步骤将自动完成，请先对文档进行定稿！");
                            //2009-07-21 wangf add 如果公文流程未定稿选择自动归档，如果下一处理步骤有多个，则不自动关闭窗口；  
                            if(objStep.options.length>1) return;
                            parent.window.close();
                        }
                    }
                    else {
                        $id("tdAuditor").className = "";
                        $id("txtNextAuditors").value = "";
                        $id("txtNextAuditorNames").value = "";
                        $id("txtNextAuditorNames").readOnly = true;
                        $id("txtNextAuditorNames").style.backgroundColor = "transparent";
                        $id("txtNextAuditorNames").style.borderColor = "#cccccc";
                        $id("selectuser").style.display = "none";
                        $id("selectauditor").style.display = "none";
                        $id("notselect").style.display = "";
                        tdAlert.innerText = "";

                        //添加归档抄送人
                        var bookcc = Dom.documentElement.selectSingleNode("BookCC");
                        if (bookcc != null && bookcc.getAttribute("CCUserGUID").length > 0) {
                            var cc;
                            var arr_bookcc = bookcc.getAttribute("CCUserGUID").split(";");
                            var arr_bookccname = bookcc.getAttribute("CCUserName").split(";");

                            for (var i = 0; i < arr_bookcc.length; i++) {
                                cc = $id("txtCCUserGUID").value;
                                if (cc.indexOf(arr_bookcc[i]) < 0) {
                                    $id("txtCCUserGUID").value += (cc.length > 0 ? ";" : "") + arr_bookcc[i];
                                    $id("txtCCUserName").value += (cc.length > 0 ? ";" : "") + arr_bookccname[i];
                                }
                            }
                        }
                        return;
                    }
                }

            }
    }
    delete (Dom);
}


//功能：返回
function oprReturn() {
    //判断是否自动归档，如果“是”还需判断是否定稿
    if (isBook == "1" && sMode == "NextToBook") {
        if (isFinalize != "1" && ($id("__DocType").value == "1" || $id("__DocType").value == "3")) {

            if ($id("__type").value == "[发起人指派责任人]") {
                alert("归档步骤将自动完成，请指回当前步骤对文档进行定稿！");
            }
            else {
                alert("请先对文档进行定稿！");

            }
            return false;
        }
    }

    if (sMode == "") {
        alert("请选择下一步骤！")
        return false;
    }

    if (($id("txtNextAuditorNames").value == "") && $id("tdAuditor").className == "req") {
        alert("请选择下一步骤的责任人！")
        return false;
    }

    var mask = new StringBuilder();
    mask.append($id("sendMail").checked ? "1" : "0");
    mask.append($id("sendSMS").checked ? "1" : "0");
    var XML = new StringBuilder();

    //2、流程审批的返回，返回XML				
    XML.append("<Context>");
    //<!--"ApprovePass、CounterSignPass、ReInitiate、InitiatorAssign、ReHandle"-->
    XML.append("<Handle type='" + actionType + "'>");

    //下一步节点
    if ($id("ddlNextStep").options[$id("ddlNextStep").selectedIndex].value != "") {
        XML.append("<Action type='" + sMode + "'>");

        XML.append("<NextStepGUID>" + $id("ddlNextStep").options[$id("ddlNextStep").selectedIndex].value + "</NextStepGUID>");

        if ($id("txtNextAuditors").value != "") {
            //责任人
            XML.append("<Auditors>");

            var arrAuditor = [], arrAuditorName = [];

            arrAuditor = $id("txtNextAuditors").value.split(";");
            arrAuditorName = $id("txtNextAuditorNames").value.split(";");

            for (var i = 0, j = arrAuditor.length; i < j; i++)
                XML.append("<User guid='" + arrAuditor[i] + "' name='" + arrAuditorName[i] + "'/>");

            XML.append("</Auditors>");
        }

        XML.append("</Action>");
    }
    XML.append("</Handle>");

    //消息节点
    XML.append("<MsgMask>" + mask.toString() + "</MsgMask>");
    //抄送节点
    XML.append("<CC>");
    if ($id("txtCCUserGUID").value != "") {
        var arrCCGuid = [], arrCCName = [];

        arrCCGuid = $id("txtCCUserGUID").value.split(";");
        arrCCName = $id("txtCCUserName").value.split(";");

        for (var i = 0, j = arrCCGuid.length; i < j; i++)
            XML.append("<User guid='" + arrCCGuid[i] + "' name='" + arrCCName[i] + "'/>");
    }
    XML.append("</CC>");
    XML.append("</Context>");

    parent.window.returnValue = XML.toString();
    parent.window.close();
}

//功能：计算流转条件表达式
function Calculate(sExpStru, domainXmlDoc) {
    if (domainXmlDoc != undefined && domainXmlDoc.xml) {
        try {
            if (Trim(sExpStru).length == 0) sExpStru = "<ExpressionStru/>";

            var xmlDom = XMLDOM(); //建立XMLDOM对象

            xmlDom.loadXML(sExpStru);

            var items = xmlDom.selectNodes("//filter");

            if (items.length > 0) {
                var exp = new StringBuilder();
                for (var i = 0; i < items.length; i++) {
                    var domainType = domainXmlDoc.selectSingleNode("/BusinessType/Item/Domain[@name='" + items[i].attributes.getNamedItem("domain").text + "']").attributes.getNamedItem("type").text;
                    var domainValue = domainXmlDoc.selectSingleNode("/BusinessType/Item/Domain[@name='" + items[i].attributes.getNamedItem("domain").text + "']").text.toUpperCase();

                    //单引号
                    var sQuot;
                    switch (domainType) {
                        case "smallint":
                        case "int":
                        case "money":
                        case "tinyint":
                        case "numeric":
                        case "bit":
                        case "decimal":
                            sQuot = "";
                            //20090520 wangf modify ：如果是数值类型，表单域中未填，应该默认为“0”
                            if (Trim(domainValue) == "") domainValue = "0";
                            else domainValue = domainValue.replace(/,/ig, "");
                            break;
                        default:
                            sQuot = "'";
                            break;
                    }
                    //测试表达式
                    exp.append(" ");
                    exp.append(items[i].attributes.getNamedItem("leftparenthesis").text);
                    exp.append(" ");
                    exp.append(sQuot);
                    exp.append(domainValue);
                    exp.append(sQuot);
                    exp.append(" ")
                    exp.append(items[i].attributes.getNamedItem("opr").text);

                    var sValue, sDomain;
                    sValue = items[i].attributes.getNamedItem("value").text;

                    if (sValue.indexOf("[") > -1 && sValue.indexOf("[") > -1) {
                        sDomain = sValue.substring(sValue.indexOf("[") + 1, sValue.indexOf("]"))

                        sValue = domainXmlDoc.selectSingleNode("/BusinessType/Item/Domain[@name='" + sDomain + "']").text;
                    }
                    else {
                        sValue = items[i].attributes.getNamedItem("value").text
                    }

                    exp.append(" ");
                    exp.append(sQuot);
                    exp.append(sValue.toUpperCase());
                    exp.append(sQuot);
                    exp.append(" ");
                    exp.append(items[i].attributes.getNamedItem("rightparenthesis").text);
                    if (i < items.length - 1) {
                        exp.append(" ");
                        exp.append(items[i].attributes.getNamedItem("logic").text);
                    }
                }

                try {
                    if (eval(exp.toString())) {
                        return "true";
                    }
                    else {
                        return "false";
                    }
                }
                catch (e) {
                    return "false";
                }
            }
            else {
                return "null";
            }
        }
        catch (e) {
            return "null";
        }
    }
    else {
        return "null";
    }
}

// 功能：打开选择下一步骤责任人的窗口
function openSelectStepBandWin() {
    var sTitle = "选择步骤责任人";
    var sURL = "/MyWorkflow/WF_ProcessInitiate_Form_SelectNextStep_SelectStepBand.aspx";
    var iWidth = 360;
    var iHeight = 500;

    var sNextStepGUID = "";
    if ($id("ddlNextStep").selectedIndex < 0) return;

    sNextStepGUID = $id("ddlNextStep").options[$id("ddlNextStep").selectedIndex].value;

    var sParams = new StringBuilder();


    sParams.append("stepguid=" + sNextStepGUID);
    //需要考虑到发起人指派的情况；发起人指派，上一岗位，直接取当前步骤所有节点的岗位字符串；
    if ($id("__lastStationGUID") != undefined) {
        sParams.append("&lastStationGUID=" + ($id("__type").value != "[发起人指派责任人]" ? $id("__lastStationGUID").value : ""));
    }

    var args = [document.getElementById("txtNextAuditors").value, _domainXml];

    var arrStr = openMyDlg(sTitle, sURL, sParams.toString(), args, iWidth, iHeight);

    if (arrStr != undefined) {
        if (arrStr.length > 0) {
            $id("txtNextAuditors").value = arrStr[0];
            $id("txtNextAuditorNames").value = arrStr[1];
            tdAlert.innerText = "";
        }
    }

}

//功能：选择用户
function OpenSelectMultiUser(o) {
    var url;
    var sOids, sNames;
    var sHeight, sWidth;
    var objValue = document.getElementById(o.valueid);
    var objName = document.getElementById(o.textid);
    if (objValue == undefined) {
        sOids = "";
    }
    else {
        sOids = objValue.value;
    }

    if (objName == undefined) {
        sNames = "";
    }
    else {
        sNames = objName.value;
    }

    if (o.ismultiselect == "true") {
        sHeight = "528px";
        sWidth = "720px";
        if (o.isallowsort == "true") {
            url = "/_controls/lookup//SelectUserMulti.aspx?class=user&browse=0&rowslimit=" + o.rowslimit; //只需要修改这一句
        }
        else {
            //url = "/_controls/lookup/map_selectUserMulti.aspx?class=user&browse=0&rowslimit=" + o.rowslimit;
            url = "/myWorkflow/Interface/Pub/selectUserMulti.aspx?class=user&browse=0&rowslimit=" + o.rowslimit;
        }
    }
    else {
        sHeight = "500px";
        sWidth = "520px";
        url = "/_controls/lookup/map_selectUserSingle.aspx?class=user&browse=0"
    }

    var args = new LookupArgsClass();
    var item;
    var arrValue = sOids.split(";");
    var arrName = sNames.split(";");
    var arrItem = new Array();
    if (arrValue.length == arrValue.length) {
        for (i = 0; i < arrValue.length; i++) {
            item = document.createElement("span");
            item.oid = arrValue[i];
            item.otype = "";
            item.innerHTML = "<span><img class='lui' src='/_imgs/ico_16_16.gif'>" + arrName[i] + "</span>";
            arrItem[i] = item;
        }
    }


    args.items = arrItem;
    var lookupItems = window.showModalDialog(url, args, "dialogWidth:" + sWidth + ";dialogHeight:" + sHeight);
    var sText = "";
    var sValue = "";
    var sType = "";

    if (lookupItems != null && o.textid != null && o.valueid != null) {
        if (lookupItems.items.length > 0) {
            for (var i = 0; i < lookupItems.items.length; i++) {
                sText += (sText == "" ? "" : ";") + lookupItems.items[i].name;
                sValue += (sValue == "" ? "" : ";") + lookupItems.items[i].id;
                sType += (sType == "" ? "5" : ";5");
            }
        }

        document.all(o.textid).value = sText;
        document.all(o.valueid).value = sValue;
        tdAlert.innerText = "";
    }
}